package cn.admobiletop.adsuyi.adapter.toutiao.loader;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;

import cn.admobiletop.adsuyi.ADSuyiSdk;
import cn.admobiletop.adsuyi.ad.ADSuyiBannerAd;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterLoader;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterParams;
import cn.admobiletop.adsuyi.ad.data.ADSuyiPlatformPosId;
import cn.admobiletop.adsuyi.ad.entity.ADSuyiAdSize;
import cn.admobiletop.adsuyi.ad.error.ADSuyiError;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiBannerAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.ADSuyiIniter;
import cn.admobiletop.adsuyi.adapter.toutiao.listener.BannerAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.manager.TTAdInitManager;
import cn.admobiletop.adsuyi.adapter.toutiao.util.TTAdUtil;
import cn.admobiletop.adsuyi.config.ADSuyiInitConfig;
import cn.admobiletop.adsuyi.util.ADSuyiAdUtil;

/**
 * @author ciba
 * @description Banner广告加载器
 * @date 2020/4/2
 */
public class BannerAdLoader implements ADSuyiAdapterLoader<ADSuyiBannerAd, ADSuyiBannerAdListener> {
    private BannerAdListener bannerAdListener;

    @Override
    public void loadAd(ADSuyiBannerAd bannerAd, ADSuyiAdapterParams adapterParams, ADSuyiBannerAdListener adListener) {
        if (!ADSuyiAdUtil.isReleased(bannerAd)
                && bannerAd.getContainer() != null
                && adapterParams != null
                && adapterParams.getPlatformPosId() != null
                && adListener != null) {
            ADSuyiPlatformPosId platformPosId = adapterParams.getPlatformPosId();

            ADSuyiInitConfig config = ADSuyiSdk.getInstance().getConfig();
            if (config != null && config.isFilterThirdQuestion()) {
                if (TTAdUtil.withoutLowSystem()) {
                    adListener.onAdFailed(ADSuyiError.createErrorDesc(ADSuyiIniter.PLATFORM, platformPosId.getPlatformPosId(), -1, "过滤Banner广告，经过测试头条的模板广告在安卓5.1及以下版本的手机上可能存在兼容性问题"));
                    return;
                }
            }

            TTAdNative ttAdNative = TTAdInitManager.getInstance().createNative(bannerAd.getActivity());
            if (ttAdNative == null) {
                adListener.onAdFailed(ADSuyiError.createErrorDesc(ADSuyiIniter.PLATFORM, platformPosId.getPlatformPosId(), -1, "头条SDK createNative失败，可能初始化失败或初始化数据有误"));
                return;
            }

            ADSuyiAdSize adSize = platformPosId.getAdSize() == null ? new ADSuyiAdSize(640, 100) : platformPosId.getAdSize();

            float initiallyDensity = ADSuyiSdk.getInstance().getInitiallyDensity();
            int width = (int) (bannerAd.getContainer().getWidth() / initiallyDensity);
            int height = width * adSize.getHeight() / adSize.getWidth();

            AdSlot adSlot = new AdSlot.Builder()
                    .setCodeId(platformPosId.getPlatformPosId())
                    .setSupportDeepLink(true)
                    .setAdCount(1)
                    .setExpressViewAcceptedSize(width, height)
                    .setImageAcceptedSize(adSize.getWidth(), adSize.getHeight())
                    .setDownloadType(TTAdUtil.getDisplayDownloadTipsDialogType())
                    .build();

            bannerAdListener = new BannerAdListener(bannerAd, platformPosId.getPlatformPosId(), adListener);
            ttAdNative.loadBannerExpressAd(adSlot, bannerAdListener);
        }
    }

    @Override
    public void onResumed() {

    }

    @Override
    public void onPaused() {

    }

    @Override
    public void release() {
        if (bannerAdListener != null) {
            bannerAdListener.release();
            bannerAdListener = null;
        }
    }
}
